import axios from 'axios';
import {useRouter} from 'vue-router';

// 创建 axios 实例
const api = axios.create({
  baseURL: 'http://localhost：8090', // 替换为实际的 API 基础 URL
  timeout: 5000
});

// 定义计价规则相关的操作
class PriceService {
  private router = useRouter();

  // 添加计价规则
  async addPriceRule(priceRule: any) {
    try {
      const response = await api.post('/price-rule/add', priceRule);
      // 假设添加成功后跳转到列表页
      //this.router.push('/price-rule/list');
      return response.data;
    } catch (error) {
      console.error('添加计价规则失败', error);
      throw error;
    }
  }

  // 编辑计价规则
  async updatePriceRule(priceRule: any) {
    try {
      const response = await api.post(`/price-rule/edit`, priceRule);
      return response.data;
    } catch (error) {
      console.error('编辑计价规则失败', error);
      throw error;
    }
  }

  // 条件查询价格
  async queryPriceRule(priceRule: any) {
    try {
      const response = await api.post('/price-rule/query',priceRule);
      // return response.data;
      console.log("周豪",response);
      return response;
    }catch (error){

      console.error('查询失败',error);
      throw error;
    }
  }

  // 删除计价规则
  async deletePriceRule(id: number) {
    try {
      const response = await api.delete(`/price-rule/delete/${id}`);
      return response.data;
    } catch (error) {
      console.error('删除计价规则失败', error);
      throw error;
    }
  }

}


export const priceService = new PriceService();
